public class Solution654 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return build(nums,0,nums.length-1);
    }

    private TreeNode build(int[] nums, int start, int end) {
        if (start>end){
            return null;
        }
        int max=nums[start],pos=start;
        for (int i=start+1;i<=end;i++){
            if (nums[i]>max){
                max=nums[i];
                pos=i;
            }
        }
        TreeNode treeNode=new TreeNode(max);
        treeNode.left=build(nums,start,pos-1);
        treeNode.right=build(nums,pos+1,end);
        return treeNode;
    }

    public static void main(String[] args) {
        System.out.println(new Solution654().constructMaximumBinaryTree(new int[]{3,2,1,6,0,5}).right.left.val);
    }
}
